package com.wuyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyb.enums.ErrorCode;
import com.wuyb.mapper.MeetingScreenshotMapper;
import com.wuyb.pojo.MeetingScreenshot;
import com.wuyb.pojo.config.ImgPathPro;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.entity.ResultUtils;
import com.wuyb.pojo.param.ImgBase64Param;
import com.wuyb.service.MeetingScreenshotService;
import com.wuyb.util.FileUtil;
import com.wuyb.util.MyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wyb
 * @Date: 2024/01/02/16:26
 * @Description:
 */
@Service
@Slf4j
public class MeetingScreenshotServiceImpl extends ServiceImpl<MeetingScreenshotMapper, MeetingScreenshot> implements MeetingScreenshotService {

    @Resource
    private MeetingScreenshotMapper meetingScreenshotMapper;

    @Resource
    private ImgPathPro imgPathPro;

    @Resource
    private FileUtil fileUtil;


    private int insert(String meetingId, Integer userId, String fileName) {
        MeetingScreenshot meetingScreenshot = new MeetingScreenshot().setUserId(userId).setMeetingId(meetingId).setPath(fileName);
        return meetingScreenshotMapper.insert(meetingScreenshot);
    }

    @Override
    public BaseResponse<?> setScreenshotWithBase64(String mid, ImgBase64Param imgBase64Param) {
        String filename = fileUtil.uploadImgWithBase64(imgBase64Param.getImgBase64(), imgPathPro.getScreenshot());
        if (filename != null) {
            int result = insert(mid, MyUtil.getUser().getId(), filename);
            if (result > 0) {
                return ResultUtils.success(ErrorCode.SUCCESS);
            }
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(),"保存失败");
    }

    @Override
    public BaseResponse<?> selectPath(String meetingId) {
        QueryWrapper<MeetingScreenshot> meetingWrapper = new QueryWrapper<MeetingScreenshot>().select("path_xq")
                .eq("meeeting_id_xq", meetingId)
                .eq("user_id_xq", MyUtil.getUser().getId());
        List<Object> objects = meetingScreenshotMapper.selectObjs(meetingWrapper);
        return ResultUtils.success(objects);
    }

    @Override
    public BaseResponse<?> selectAll() {
        return ResultUtils.success(meetingScreenshotMapper.selectAll(MyUtil.getUser().getId()));
    }

    @Override
    public BaseResponse<?> deleteById(String id) {
        int result = meetingScreenshotMapper.deleteById(id);
        if (result > 0) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(), "删除失败");
    }
}
